package leetcode;

import java.util.Arrays;

/**
 * 描述:977. 有序数组的平方
 *
 * @author Madison You
 * @created 23:33
 */
public class Easy_977_SortedSquares {

    public static void main(String[] args) {
        int[] ints = {-7, -3, 2, 3, 11};
        new Easy_977_SortedSquares().sortedSquares(ints);
    }

    public int[] sortedSquares(int[] nums) {
        int[] ints = new int[nums.length];
        int left = 0;
        int right = nums.length - 1;
        int t = 0;
        while (left <= right) {
            if (Math.abs(nums[left]) < Math.abs(nums[right])) {
                ints[t++] = nums[right] * nums[right];
                right--;
            }else {
                ints[t++] = nums[left] * nums[left];
                left++;
            }
        }
        int[] arr = new int[ints.length];
        for (int i = 0; i < ints.length; i++) {
            arr[ints.length - 1 - i] = ints[i];
        }
        return arr;
    }
}
